<?php

class Core_Component_System_Business_Component {
    
    public function createDirectory($component){
        $path = DP_CODE.'Core'.DS.'Component'.DS.ucfirst($component->getCode()).DS;
        Soul_Util_FS::makeDirectory($path);
        Soul_Util_FS::makeDirectory($path.'Controller');
        Soul_Util_FS::makeDirectory($path.'Business');
        Soul_Util_FS::makeDirectory($path.'Model');
        Soul_Util_FS::makeDirectory($path.'Method');
        Soul_Util_FS::makeDirectory($path.'Event');
        Soul_Util_FS::writeFile('', $path.'config.yml');
        Soul_Util_FS::writeFile('<?php', $path.'initialize.php');
        Soul_Util_FS::writeFile('<?php', $path.'install.php');
        Soul_Util_FS::writeFile('<?php', $path.'uninstall.php');
        
        $lang = array('lang_code'=>'zh-cn');
        Soul_Util_FS::writeFile('', DP_LANG.$lang['lang_code'].DS.strtolower($component->getCode()).'.yml');
        
        if($component->getIsBackend()){
            Soul_Util_FS::makeDirectory($path.'Controller'.DS.'Backend');
            Soul_Util_FS::makeDirectory(DP_DESIGN.'backend'.DS.'default'.DS.'component'.DS.strtolower($component->getCode()));
        }
        if($component->getIsFrontend()){
            Soul_Util_FS::makeDirectory($path.'Controller'.DS.'Frontend');
            Soul_Util_FS::makeDirectory(DP_DESIGN.'frontend'.DS.'default'.DS.'component'.DS.strtolower($component->getCode()));
        }
        if($component->getIsMember()){
            Soul_Util_FS::makeDirectory($path.'Controller'.DS.'Member');
            Soul_Util_FS::makeDirectory(DP_DESIGN.'member'.DS.'default'.DS.'component'.DS.strtolower($component->getCode()));
        }        
    }
    
    public function uninstall($component){
        $path = DP_CODE.'Core'.DS.'Component'.DS.ucfirst($component->getCode()).DS;
        if(file_exists($path.'uninstall.php')){
            include_once $path.'uninstall.php';
        }
        Soul_Util_FS::removeDirectory($path);
        Soul_Util_FS::removeDirectory(DP_DESIGN.'backend'.DS.'default'.DS.'component'.DS.strtolower($component->getCode()));
        Soul_Util_FS::removeDirectory(DP_DESIGN.'frontend'.DS.'default'.DS.'component'.DS.strtolower($component->getCode()));
        Soul_Util_FS::removeDirectory(DP_DESIGN.'member'.DS.'default'.DS.'component'.DS.strtolower($component->getCode()));
        $langs = App::getModel('system/localize')->getQuery()->fetch()->toArray();
        foreach($langs as $lang){
            Soul_Util_FS::deleteFile(DP_LANG.$lang['lang_code'].DS.strtolower($component->getCode()).'.yml');
        }
    }

    public function install($code){
        if(!is_dir(DP_CODE.'Core'.DS.'Component'.DS.$code)){
            throw new Core_Component_System_Exception(Core_Component_System_Exception::COMPONENT_NOT_FOUND);
        }
        if(!file_exists(DP_CODE.'Core'.DS.'Component'.DS.$code.DS.'install.php')){
            throw new Core_Component_System_Exception(Core_Component_System_Exception::COMPONENT_NOT_FOUND_INSTALL);
        }

        try{
            include_once DP_CODE.'Core'.DS.'Component'.DS.$code.DS.'install.php';
//            $db = $GLOBALS[RC]->getDb();
//            $db->beginTransaction();
//            if(isset($componentSql)){
//                $db->setSql($componentSql)->execute();
//            }
//            $model = App::getModel('system/component');
//            $model->addData($componentInformation)->setDb($db)->save();
//            $db->commitTransaction();
        }catch(Exception $ex){
//            $db->rollbackTransaction();
            throw new Core_Component_System_Exception(Core_Component_System_Exception::COMPONENT_INSTALL_FAIL);
        }
    }   
}